Adding and Removing Panels Dynamically

Description

You can add and remove panels using Javascript, without having to place [Panel Card] containers in a [Panel Navigator]. This guide demonstrates how to build a UX component that can dynamically add panels that have a designated panel number defined by the user.

images/dyn28.png
A dynamically added Panel.

To see how to dynamically add and remove panels follow the guide below or watch the following video:

Create a UX that Dynamically Adds and Removes Panels

  1. In the UX Builder open the UX Controls page and check the Mobile checkbox.

    images/dyn.png
  2. Open the Panels Menu and click on [Panel Navigator] to add a Panel Navigator to the component.

    images/dyn2.png
  3. Highlight the Panel Navigator. In the Panels menu click the [Panel Card] option and click 'Insert After'.

    images/dyn3.png
  4. Highlight [Panel Card End:PANELCARD_1]. Click on the [Panel Card] option and 'Insert After again to add a second panel card to the Panel Navigator.

    images/dyn4.png
  5. Highlight [Panel Card End: PANELCARD_2]. Click on the [Panel Card] option in the Panels menu and click 'Insert After. The component should now look like this:

    images/dyn5.png
  6. Highlight PANELCARD_1. Click on [Static Text] in the 'Other Controls' menu to add a Static text control to PANELCARD_1.

    images/dyn6.png
  7. Highlight the Static Text control. In Static Text Properties section on the right set the 'Static text property' to read 'Panel 1'

    images/dyn7.png
  8. Highlight PANELCARD_2. Add a [Static Text] control to PANELCARD_2 and set the 'Static text' property to read 'Panel 2'.

    images/dyn8.png
  9. Highlight PANELCARD_3. Add a third [Static Text] control and se 'Static text' to read 'Panel 3'.

    images/dyn9.png
  10. Highlight the Panel Navigator

    images/dyn10.png
  11. In the Panel Navigator's properties list locate the Navigator Position Indicator section, check the 'Has indicator' checkbox.

    images/dyn11.png
  12. Open the Containers menu and click on the [Container] option.

    images/dyn12.png
  13. From the Container Type list select 'PanelHeader' and click 'Insert After'.

    images/dyn13.png
  14. Highlight the newly added Panel Header. Open the 'Other Controls' menu and click on [Button] to add a button control to the panel header.

    images/dyn14.png
  15. In the button control's properties list change the 'Button text' property to read 'Add Panel'.

    images/dyn15.png
  16. Scroll down the properties list for the 'Add Panel' button to the 'Javascript' section. Click the [...] button next to the onClick property.

    images/dyn16.png
  17. In the Edit onClick Event dialog select 'Text mode' from the radio buttons in the Toolbar, then add the following javascript and click 'Save'.

    addPanel();
    images/dyn17.png
  18. Open the Other Controls menu again and click [Button] to add a second button control to the Panel Header.

    images/dyn18.png
  19. Highlight this button and in the properties list set it's 'Button text' property to read 'Remove Panel'.

    images/dyn19.png
  20. Scroll down the properties list for the Remove Panel button. Again in the Javascript section click on the onClick property.

    images/dyn20.png
  21. In the Edit onClick Event select the 'Text mode' radio button again and add the following javascript and click 'Save'.

    removePanel();
    images/dyn21.png
  22. Open the Data Controls menu and click on [TextBox] to add a textbox control to the panel header. Give this textbox control the name 'panelNum'

    images/dyn22.png
  23. Open the Javascript functions page in the Code menu.

    images/dyn23.png
  24. Add the following Javascript to define the addPanel() and removePanel() functions:

    function addPanel() {
         var id = {dialog.object}.getValue('panelNum');
         if(id=='') {
         alert('Please type a number in the textbox before clicking the button.');
         return false;
         }
         
         //specify a unique name for the new Panel.
         var panelName = 'NEWPANEL_' + id;
         var panelTitle = 'Pane' + id;
         
         //define a new Panel Card
         var myNewPanelCard = new A5.PanelCard({
              theme : '(dialog.style)',
              body: {
                   content: {
                        type: 'html',
                        src: 'this is my new panel text for:' + panelName
                        }
                    }
                })
          
          //get a pointer to the Panel Navigator where the new Panel will be added
          
          var pNav = {dialog.object}.panelGet('PANELNAVIGATOR_1');
          
          //add the new Panel Card to the Panel Navigator
          
          pNav.addPanel({
              name: panelName,
              title: panelTitle,
              src: myNewPanelCard
          });
          
          //optionally set focus to the Panel just added.
          pNav.setActivePanel(panelName);
          }
    
    function removePanel() {
         var id = {dialog.object}.getValue('panelNum');
         if(id == '') {
             alert('Please type a number in the textbox before clicking the button');
             return false;
         }
         var panelName = 'NEWPANEL_' + id;
         var pNav = {dialog.object}.panelGet('PANELNAVIGATOR_1');
         pNav.removePanel(panelName);
         }
    images/dyn24.png
  25. Run the component in Live Preview. When you try to add a page without entering any information into the textbox you should get an alert.

    images/dyn25.png
  26. When a number is entered into the textbox and 'Add Panel' is clicked, then a new panel should appear.

    images/dyn26.png
  27. Now try removing the added panel.

    images/dyn27.png